<?php

namespace Gql\Tools;


use Gql\Core\Context;
use Gql\Core\With;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\NodeList;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Schema;

class Utils
{

    /**
     * 解析 Schema
     * @param Schema $schema
     * @return array
     */
    public static function parseSchema(Schema $schema): array
    {
        $query = $schema->getQueryType();
        $mutation = $schema->getMutationType();
        $res = [
            'Query' => [],
            'Mutation' => [],
        ];
        if (!is_null($query)) {
            $fields = $query->getFields();
            foreach ($fields as $field) {
                $type = $field->getType();
                $res['Query'][] = [
                    'name' => $field->name,
                    'description' => $field->description,
                    'type' => [
                        'name' => $type->name,
                        'description' => $type->description,
                    ],
                    'args' => $field->args
                ];
            }
        }
        if (!is_null($mutation)) {
            $fields = $mutation->getFields();
            foreach ($fields as $field) {
                $type = $field->getType();
                $res['Mutation'][] = [
                    'name' => $field->name,
                    'description' => $field->description,
                    'type' => [
                        'name' => $type->name,
                        'description' => $type->description,
                    ],
                    'args' => $field->args
                ];
            }
        }
        return $res;
    }

    /**
     * 解析返回 SDL语言
     * @param string $entryPath 入口文件
     * @return string
     */
    public static function parseTypes(string $entryPath): string
    {
        return (new Parse($entryPath))->parse();
    }


}